"""
    Author:
    Date: 2019-07-17

    微信管理后台-考核指标
"""

from django.shortcuts import render

from entities import app_mgr
from entities import user_mgr
from utils import util

from .. import helpers
from .. helpers import gma_login_required
from entities.consts import *


@gma_login_required
def index(request):
    """ 首页 """
    # context
    context = helpers.build_context(request)
    context['total_channel_count'] = len(app_mgr.get_all_channel_list())
    context['total_app_count'] = 0
    return render(request, 'gma/assess/assess_home.html', context)


@gma_login_required
def assess_channel_list(request):
    """ 待评估的频道列表 """
    context = helpers.build_context(request)
    context['channels'] = app_mgr.get_all_channel_list()
    return render(request, 'gma/assess/assess_channels.html', context)


@gma_login_required
def assess_platform(request):
    """ 按天评估平台 """
    context = helpers.build_context(request)
    context['days'] = util.get_last_days_info(150)
    return render(request, 'gma/assess/assess_platform.html', context)


@gma_login_required
def assess_detail_manual(request):
    print(f'day, channel, new_user_count, new_pay_user_count, old_pay_user_count, repay_user_count, PUR, RPUR, pay_amount, new_pay_amount, old_pay_amount, ARPU, ARPPU')

    # channel = 'iOS_QuYue'
    # channel = 'huawei'
    # channel = 'yx_oppo'
    # channel = 'yx_huawei'
    # channel = 'yx_vivo'
    # channel = 'yj_origin'
    # channel = 'iOS_YueJian'
    channel = 'vivo'
    # days = util.get_last_days_info(80)
    days = util.get_last_days_info(200)
    days.reverse()
    for (day, _, _) in days:
        info = user_mgr.get_assess_info(channel, day)
        # 用户分析
        new_user_count = info['new_user_count']
        new_pay_user_count = info['new_pay_user_count']
        old_pay_user_count = info['old_pay_user_count']
        repay_user_count = info['repay_user_count']
        PUR = round(info['PUR']/100.0, 2)
        RPUR = round(info['RPUR']/100.0, 2)

        # 充值分析
        pay_amount = info['pay_amount']
        new_pay_amount = info['new_pay_amount']
        old_pay_amount = info['old_pay_amount']
        ARPU = round(info['ARPU'], 2)
        ARPPU = round(info['ARPPU'], 2)

        print(f'{day},{channel},{new_user_count},{new_pay_user_count},{old_pay_user_count},{repay_user_count},{PUR},{RPUR},{pay_amount},{new_pay_amount},{old_pay_amount},{ARPU},{ARPPU}')


@gma_login_required
def assess_detail(request, day, channel):
    """ 评估详情 """
    # # 新增用户数量
    # new_user_count = user_mgr.get_new_user_count_of_channel(channel, day)
    # # 新增付费用户数量
    # new_pay_user_count = user_mgr.get_new_pay_user_count_of_channel(channel, day)
    # # 老的付费用户数量
    # old_pay_user_count = user_mgr.get_old_pay_user_count_of_channel(channel, day)
    # # 新增用户充值数量
    # pay_amount = user_mgr.get_total_pay_amount_of_channel(channel, day)
    # # 新增用户充值数量
    # new_pay_amount = user_mgr.get_new_user_total_pay_amount_of_channel(channel, day)
    # # 老用户充值数量
    # old_pay_amount = user_mgr.get_old_user_total_pay_amount_of_channel(channel, day)
    # # 复充的用户数量
    # repay_user_count = user_mgr.get_repay_user_count_of_channel(channel, day)

    info = user_mgr.get_assess_info(channel, day)

    context = helpers.build_context(request)
    context['channel'] = channel if channel else "全部"
    context['day'] = day

    # # 用户分析
    # context['new_user_count'] = info['new_user_count']
    # context['new_pay_user_count'] = info['new_pay_user_count']
    # context['old_pay_user_count'] = info['old_pay_user_count']
    # context['repay_user_count'] = info['repay_user_count']
    # context['PUR'] = info['PUR']
    # context['RPUR'] = info['RPUR']
    #
    # # 充值分析
    # context['pay_amount'] = pay_amount
    # context['old_pay_amount'] = old_pay_amount
    # context['new_pay_amount'] = new_pay_amount
    # context['ARPU'] = (new_pay_amount/new_user_count) if new_user_count > 0 else 0
    # context['ARPPU'] = (new_pay_amount/new_pay_user_count) if new_pay_user_count > 0 else 0

    context['info'] = info
    return render(request, 'gma/assess/assess_detail.html', context)


if __name__ == '__main__':
    print('ok')
